var express = require("express");
var router = express.Router();
var commentService = require("../../service/blog/commentService");
var userinfoService = require("../../service/auth/userinfoService");
var moment = require("moment");
var util = require('../../utils/util')
const authMiddleware = require("../../middleware/authMiddleware");
//评论列表
router.get(
    "/list",
    async (req, res, next) => {
        let list = await commentService.query(req.query);
        res.json({ code: 1, data: list });
    },
    (err) => {
        res.json({ code: 0, msg: err });
    }
);
//新增评论
router.post(
    "/add",
    authMiddleware.verifyToken,
    async (req, res, next) => {
        const user = await userinfoService.query({ _id: req._id })
        let data = {
            avatar: req.body.avatar || '',
            name: user[0].username || '游客',
            blog_id: req.body.blog_id,
            userId: req._id,
            parent_id: req.body.parent_id || null,
            root_id: req.body.root_id || null,
            createTime: moment().format('YYYY-MM-DD HH:mm:ss'),
            content: req.body.content,
        }
        commentService.add(data).then(() => {
            res.json({ code: 1, msg: "评论成功" });
        });
    },
    (err) => {
        res.json({ code: 0, msg: err });
    }
);
//更新博客
router.post(
    "/update",
    authMiddleware.verifyToken,
    async (req, res, next) => {
        commentService.update(req.body).then(() => { res.json({ code: 1, msg: "更新成功" }) });
    },
    (err) => {
        res.json({ code: 0, msg: err });
    }
);
//删除菜单
router.post(
    "/remove",
    authMiddleware.verifyToken,
    async (req, res, next) => {
        let { _id } = req.body;
        if (_id == "" || !_id) return res.json({ code: 0, msg: "_id不存在" });
        commentService.delete({ _id: _id }).then(() => {
            res.json({ code: 1, msg: "删除成功" });
        });
    },
    (err) => {
        res.json({ code: 0, msg: err });
    }
);
//查询
router.get(
    "/model",
    async (req, res, next) => {
        let { _id } = req.query;
        let list = await commentService.query({ _id });
        res.json({ code: 1, data: list.length > 0 ? list[0] : {} });
    },
    (err) => {
        res.json({ code: 0, msg: err });
    }
);
module.exports = router;
